package com.laotou99.wmsjjj.wmsjjj.service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laotou99.wmsjjj.wmsjjj.bean.po.LOCPO;
import com.laotou99.wmsjjj.wmsjjj.bean.vo.LOCVO;
import com.laotou99.wmsjjj.wmsjjj.repository.LOCPORepository;

@Service("locService")
public class LOCService {

	@Autowired
	LOCPORepository locpoRepository;
	
	public boolean saveLOC(LOCVO vo) {

		if(checkLOC(vo)) {
			return true;
		}
		LOCPO locpo = new LOCPO();
		BeanUtils.copyProperties(vo, locpo);
		locpo.setCreateDate(new Date());
		
		LOCPO locpo2 = locpoRepository.save(locpo);
		System.out.println("locpo2-=>"
			+JSON.toJSONString(locpo2));
		Long l1 = locpo2.getId();
		locpo2.setLocId(l1.intValue());
		locpo2.setStatus("N");
		LOCPO locpo3 = locpoRepository.save(locpo2);
		System.out.println("locpo3-=>"
			+JSON.toJSONString(locpo3));
		return false;
	}

	private boolean checkLOC(LOCVO vo) 
	{  
		if(vo.getLocNum()==null || 
			vo.getLocNum().equals("")) 
		{
			return true;
		}
		LOCPO locpo = locpoRepository
			.findByLocNum(vo.getLocNum());
		if(locpo==null) {}else {
			return true;
		}
		return false;
	}

	public boolean updateLOC(LOCVO vo) 
	{
		if(vo.getId()==0) {
			return true;
		} 
		LOCPO po1 = new LOCPO();
		BeanUtils.copyProperties(vo, po1);
		po1.setModifyDate(new Date());
		
		LOCPO locpo2 = locpoRepository.save(po1);
		
		System.out.println("locpo2-=>"
			+JSON.toJSONString(locpo2));
		
		return false;
	}

	public boolean delLOC(long id1) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean holdLOC(long id1) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean unHoldLOC(long id1) {
		// TODO Auto-generated method stub
		return false;
	}

	public String showAllLoc() { 
		List<LOCPO> findAll = locpoRepository.findAll();
		return JSONObject.toJSONString(findAll);
	}

	public String showList(JSONObject jo1) {
		String result = "";
		
		List<LOCPO> findAll = locpoRepository.findAll();
		
		Collections.reverse(findAll);
		result = JSON.toJSONString(findAll);
		System.out.println("result-=>"+result);
		return result;
	}

	public String addNew(JSONObject jo1) {
		System.out.println("jo1-=>"+jo1.toJSONString());
		
		LOCPO lp1 = jo1.parseObject(jo1.toJSONString(), LOCPO.class);
		lp1.setCreateDate(new Date());
		lp1.setModifyDate(new Date());
		
		LOCPO lp2 = locpoRepository.save(lp1);
		System.out.println("lp2-=>"+JSON.toJSONString(lp2));
		return "OK";
	}

	public String update(JSONObject jo1) {
		System.out.println("update jo1-=>"+jo1.toJSONString());
		
		LOCPO lp1 = jo1.toJavaObject(LOCPO.class);
		lp1.setModifyDate(new Date());
		LOCPO lp2 = locpoRepository.save(lp1);
		System.out.println("lp2-=>"+JSON.toJSONString(lp2));
		
		return "OK";
	}

	public String selectById(JSONObject jo1) {
		System.out.println("jo1-=>"+jo1.toJSONString());
		Long lng1 = jo1.getLong("id");
		LOCPO findOne = locpoRepository.findOne(lng1);
		return JSON.toJSONString(findOne);
	}

	public String delete(JSONObject jo1) {
		System.out.println("jo1"+jo1.toJSONString());
		
		Long lng1 = jo1.getLong("id");
		
		locpoRepository.delete(lng1);
		return "OK";
	}

}
